package com.yanqu.road.dao.impl.activity.qqprivilege;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ppl.PplLine;
import com.yanqu.road.entity.activity.ppl.PplUserData;
import com.yanqu.road.entity.activity.qqprivilege.QQPrivilegeUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class QQPrivilegeUserDataDaoImpl extends TempDataObjectDao<QQPrivilegeUserData> {

    public static String getQQPrivilegeUserDataSql = "select * from `t_u_qq_privilege` where `activityId`=? and `userId`=?";
    public QQPrivilegeUserData getQQPrivilegeUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getQQPrivilegeUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateQQPrivilegeUserDataSql = "update `t_u_qq_privilege` set dayLoginRewardTime=?,gotRegisterReward=?,gotProgressReward=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(QQPrivilegeUserData qQPrivilegeUserData) {
        qQPrivilegeUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, qQPrivilegeUserData.getDayLoginRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getGotRegisterReward()));
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getGotProgressReward()));
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, qQPrivilegeUserData.getUserId()));
        if (getDb().executeNoneQuery(updateQQPrivilegeUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertQQPrivilegeUserDataSql = "insert into `t_u_qq_privilege`(activityId,userId,dayLoginRewardTime,gotRegisterReward,gotProgressReward) values (?,?,?,?,?)";
    @Override
    protected boolean addData(QQPrivilegeUserData qQPrivilegeUserData) {
        qQPrivilegeUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, qQPrivilegeUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, qQPrivilegeUserData.getDayLoginRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getGotRegisterReward()));
        para.put(i++, new DbParameter(Types.INTEGER, qQPrivilegeUserData.getGotProgressReward()));
        if (getDb().executeNoneQuery(insertQQPrivilegeUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected QQPrivilegeUserData parseData(ResultSet rs) throws SQLException {
        QQPrivilegeUserData qQPrivilegeUserData = new QQPrivilegeUserData();
        qQPrivilegeUserData.setActivityId(rs.getInt("activityId"));
        qQPrivilegeUserData.setUserId(rs.getLong("userId"));
        qQPrivilegeUserData.setDayLoginRewardTime(rs.getLong("dayLoginRewardTime"));
        qQPrivilegeUserData.setGotRegisterReward(rs.getInt("gotRegisterReward"));
        qQPrivilegeUserData.setGotProgressReward(rs.getInt("gotProgressReward"));

        return qQPrivilegeUserData;
    }
}
